package other;

public class MySparsityArrayTest {
    public static void main(String[] args) {
        int a[][] = {
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,1,0,0,0,0,0,0,0},
                {0,5,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,3,0,0,0},
                {0,0,0,32,0,0,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,0,9,0,0,0,0,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,0,0,199,0,0,0,0,85,0},
                {0,0,0,0,0,0,0,0,0,0,0},
                {0,0,75,0,0,0,0,0,0,0,0},
        };
        System.out.println("a---------------------------");
        printArray(a);
        int[][] b = convert(a);
        System.out.println("b---------------------------");
        printArray(b);
        int[][] c = recover(b);
        System.out.println("c---------------------------");
        printArray(c);
    }

    private static int[][] recover(int[][] a) {
        int[][] result = new int[a[0][0]][a[0][1]];
        for (int i=1;i<=a[0][2];i++){
            result[a[i][0]][a[i][1]]=a[i][2];
        }
        return result;
    }

    public static void printArray(int[][] a){
        String str="";
        for (int i=0;i<a.length;i++){
            str="";
            for(int j=0;j<a[i].length;j++){
                str+=a[i][j];
                if(j<a[i].length){
                    str+=", ";
                }
            }
            System.out.println(str);
        }
    }
    public static int[][] convert(int[][]a){
        int count = 0;
        int line=a.length;
        int row=0;

        for (int i=0;i<a.length;i++){
            row=a[i].length;
            for(int j=0;j<row;j++){
                if(a[i][j]!=0){
                    count++;
                }
            }
        }

        int[][] result = new int[count+1][3];
        result[0][0] = line;
        result[0][1] = row;
        result[0][2] = count;
        int k=1;
        for (int i=0;i<a.length;i++){
            for(int j=0;j<a[i].length;j++){
                if(a[i][j]!=0){
                    result[k][0]=i;
                    result[k][1]=j;
                    result[k][2]=a[i][j];
                    k++;
                }
            }
        }
        return result;
    }
}
